package com.youmu.shop.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youmu.shop.product.dao.ProductDao;
import com.youmu.shop.product.dao.ProductSpecificationDao;
import com.youmu.shop.product.service.ProductService;
import com.youmu.shop.product.vo.BannerVo;
import com.youmu.shop.product.vo.ProductCategoryVO;
import com.youmu.shop.product.vo.ProductVO;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductSpecificationDao productSpecificationDao;
    
	@Override
	public List<ProductCategoryVO> findProductCategory(String productName) {
		return productDao.findProductCategory(productName);
	}

	@Override
	public List<ProductVO> findProduct(Integer isNew, String productName) {
		List<ProductVO> list = new ArrayList<>();
		if(StringUtils.isBlank(productName)){
			list = productDao.findProduct(isNew,null);
		}else{
			list = productDao.findProduct(isNew,productName);
		}
		for (ProductVO productVO : list) {
			productVO.setProductSpecifications(productSpecificationDao.getProductSpecificationInfoByProductId(productVO.getId()));
		}
		return list;
	}

	@Override
	public ProductVO getProduct(Long productId) {
		ProductVO productVO = productDao.getProduct(productId);
		productVO.setProductSpecifications(productDao.getProductSpecificationValues(productId));
		productVO.setProductImages(productDao.selectProductImagesByProductId(productVO.getId()));;
		return productVO;
	}

    /** 
     * @see com.youmu.shop.product.service.ProductService#getBanners()
     */
    @Override
    public List<BannerVo> getBanners() {
        return productDao.getBanners();
    }

	@Override
	public List<ProductCategoryVO> findProductByCategoryId(Long categoryId) {
		return productDao.findProductByCategoryId(categoryId);
	}

}
